﻿using System;

namespace Eflong.Structure
{
    [Serializable]
    public class Packet : IDisposable
    {
        public uint m_dwId;  // Identify the data packet(0 if broadcasting, else bigger than 0);

        public Int64 m_time;

        private uint m_dwDataSize;  // Data buffer size;
        public Byte[ ] m_lpbyData;  // Data buffer pointer;

        public uint GetDataSize( ) { return m_dwDataSize; }

        public Byte[ ] GetpData( ) { return m_lpbyData; }

        public Packet( uint dwDataSize )
        {
            m_dwDataSize = dwDataSize;
            m_lpbyData = new byte[ dwDataSize ];
        }

        public Packet( uint dwDataSize , Byte[ ] lpbyData )
        {
            m_dwDataSize = dwDataSize;
            m_lpbyData = new byte[ dwDataSize ];
            if ( lpbyData != null )
            {
                Array.Copy( lpbyData , m_lpbyData , dwDataSize );
            }
            m_time = 0;
            m_dwId = 0;
        }

        public Packet( int dwDataSize , Byte[ ] lpbyData )
        {
            m_dwDataSize = ( uint )dwDataSize;
            m_lpbyData = new byte[ dwDataSize ];
            if ( lpbyData != null )
            {
                Array.Copy( lpbyData , m_lpbyData , dwDataSize );
            }
            m_time = 0;
            m_dwId = 0;
        }

        public Packet( Packet packet )
        {
            m_dwDataSize = packet.m_dwDataSize;
            m_lpbyData = new byte[ m_dwDataSize ];
            Array.Copy( packet.GetpData( ) , m_lpbyData , m_dwDataSize );
            m_dwId = packet.m_dwId;
            m_time = packet.m_time;
        }

        ~Packet( )
        {
            Dispose( false );
        }

        public void Dispose( )
        {
            Dispose( true );

            GC.SuppressFinalize( this );
        }

        protected virtual void Dispose( bool disposing )
        {
            if ( disposing )
            {
                // 这里是清理"托管资源"的用户代码段
                m_lpbyData = null;
            }

            // 这里是清理"非托管资源"的用户代码段
        }
    }
}